<?php
/**
 * RPC.php
 *
 * @package reepu.com
 * @version {$Id$}
 * @copyright 2009-2018 Shanghai Reepu Information Tech. Co., Ltd.
 * @license Commercial
 * @author Boolean since.
 * @datetime 2018-12-05 17:44
 * =================================================================
 * 版权所有 (C) 2009-2018 Reepu.com，并保留所有权利。
 * 网站地址:http://www.reepu.com/
 * -----------------------------------------------------------------
 * 您只能在不用于商业目的的前提下对程序代码进行修改和使用；
 * 不允许对程序代码以任何形式任何目的的再发布。
 * =================================================================
 */

namespace Reepu\Phalcon\Swoole;

use Reepu;
use Hprose;
use Phalcon;

class RPC extends Phalcon\Cli\Task
{
    protected $service = null;
    protected $explorer = null;

    protected $service_namespace = null;
    protected $service_directory = null;
    protected $service_suffix = 'Service';

    public function onConstruct()
    {
        $this->usage = new Reepu\Phalcon\Swoole\Usage();
        $this->usage->check(['swoole', 'hprose', 'phalcon']);

        // 服务发现
        if (empty($this->discover->toArray())) {
            throw new \Exception ("Must Need Config Provider" . PHP_EOL);
        }

        // 服务配置
        if (empty($this->rpc->service->toArray())) {
            throw new \Exception ("Must Need Config Service" . PHP_EOL);
        }

        // 启动服务发现
        $this->service = Reepu\Service\Handler::getInstance($this->discover->server,
            $this->discover->server_uri);
        $this->service->setServiceConfig($this->rpc->service->toArray());

        // 使用Hprose Swoole
        $this->server = new Hprose\Swoole\Server($this->service->getListenUri());
        $this->server->setErrorTypes(E_ALL);
        $this->server->setDebugEnabled();
        $this->server->setCrossDomainEnabled();
        $this->server->set($this->swoole->setting->toArray());

        $this->explorer = new Reepu\Service\Explorer();
        $this->explorer->setConfig($this->rpc->explorer->toArray());
    }


    public function startAction()
    {
        try {
            // 将扫描到有的服务类对外发布
            foreach ($this->explorer->scan() as $item) {
                $this->server->addInstanceMethods(new $item['name'] (), null, $item['alias']);
            }

            echo "Service {$this->service->getListenUri()} Register Success" . PHP_EOL;
            $this->service->register(); // 注册服务

            // 打印服务信息
            $this->printInfo();

            echo "Service Server Start Success" . PHP_EOL;
            $this->server->start(); // 启动服务
        } catch (\Exception $e) {
            echo $e->getMessage();
        }
    }

    /**
     * 打印服务信息
     */
    protected function printInfo()
    {
        $pid = getmypid();
        if (php_uname('s') != 'Darwin' &&
            !empty($this->rpc->service->name) &&
            !cli_set_process_title($this->rpc->service->name)) {
            throw new \Exception ("Unable to set process title for PID {$pid}" . PHP_EOL);
        }

        echo "Process  ID：{$pid}" . PHP_EOL;
        echo "Listen Host：{$this->service->getListenHost()}" . PHP_EOL;
        echo "Listen Port：{$this->service->getListenPort()}" . PHP_EOL;
        echo "Application Version：{$this->usage->version()}" . PHP_EOL;
    }
}